home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / What's New / • What was new 08⁄00 / Sample Code / Interapplication Comm / MoreOSL / MoreDialogs / MoreDialogs.cp next >
Encoding:
Text File  |  2000-06-23  |  6.1 KB  |  217 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MoreDialogs.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <6>     20/3/00    Quinn   Make SetDialogItemString work regardless of whether the dialog
  21.                                     is using embedding or not.
  22.          <5>      1/3/00    Quinn   SystemClick is no longer part of, or necessary in, Carbon.
  23.          <4>     1/22/99    PCG     TARGET_CARBON
  24.          <3>      1/7/99    PCG     add ToggleDialogCheckBox and MoreAppendDialogItemList
  25.          <2>    11/11/98    PCG     fix header
  26.          <1>    11/10/98    PCG     first big re-org at behest of Quinn
  27.  
  28.     Old Change History (most recent first):
  29.  
  30.          <2>     7/24/98    PCG        eliminate dependency on 'qd'
  31.          <1>     6/16/98    PCG     initial checkin
  32. */
  33.  
  34. #include <ControlDefinitions.h>
  35.  
  36. #include "MoreToolbox.h"
  37. #include "MoreAppearance.h"
  38. #include "MoreDialogs.h"
  39.  
  40. #include <Sound.h>
  41.  
  42. pascal void SaferShortenDITL (DialogPtr dialog, DialogItemIndex index)
  43. {
  44.     while (index--) ShortenDITL (dialog, 1);
  45. }
  46.  
  47. pascal DialogPtr MoreGetNewDialog (short dlogResID)
  48. {
  49.     return GetNewDialog (dlogResID, nil, kFirstWindowOfClass);
  50. }
  51.  
  52. pascal void SetDialogItemString (DialogPtr dialog, DialogItemIndex index, ConstStringPtr str)
  53. {
  54.     OSStatus   junk;
  55.     SInt16     iType; 
  56.     Rect        iRect; 
  57.     Handle     iHandle;
  58.     ControlRef control;
  59.  
  60.     if (MoreAssert (dialog && index)) {
  61.         if (str == nil) {
  62.             str = "\p";
  63.         }
  64.         
  65.         // If the dialog item is using a control rather than the pre-Appearance
  66.         // sharing of a TEHandle, we must set the text using SetControlData.
  67.         // If you don’t do this, it mostly works, but weird things happen with
  68.         // the text highlighting in edit text items.
  69.         
  70.         if (HaveAppearance() && (GetDialogItemAsControl(dialog, index, &control) == noErr) && (control != nil)) {
  71.             
  72.             junk = SetControlData(control, kControlEditTextPart,
  73.                                 kControlEditTextTextTag,
  74.                                 str[0], (void *) &str[1]);
  75.             MoreAssertQ(junk == noErr);
  76.         } else {
  77.             GetDialogItem(dialog, index, &iType, &iHandle, &iRect);
  78.             SetDialogItemText(iHandle, str);
  79.         }
  80.     }
  81. }
  82.  
  83. pascal void GetDialogItemString (DialogPtr dialog, DialogItemIndex index, Str255 str)
  84. {
  85.     if (MoreAssert (dialog && index && str))
  86.     {
  87.         short iType; Rect iRect; Handle iHandle;
  88.         GetDialogItem (dialog,index,&iType,&iHandle,&iRect);
  89.         GetDialogItemText (iHandle, str);
  90.     }
  91. }
  92.  
  93. pascal void SelectAllDialogItemText (DialogPtr dialog, DialogItemIndex index)
  94. {
  95.     SelectDialogItemText (dialog,index,0,32767);
  96. }
  97.  
  98. pascal void MoveableModalDialog (ModalFilterUPP mfp, short *itemHit)
  99. {
  100.     //
  101.     //    I just made this logic up.
  102.     //    In a perfect world, I would steal the implementation
  103.     //    of the real ModalDialog and tweak it a little bit.
  104.     //    Consider it a to-do item.
  105.     //
  106.  
  107.     EventRecord        event;
  108.     DialogRef        pop, dummy;
  109.     WindowRef        whichWindow;
  110.     short            partCode;
  111.     Boolean            handledIt = false;
  112.  
  113.     pop = GetDialogFromWindow (FrontWindow( ));
  114.     *itemHit = kDialogItemIndexNone;
  115.  
  116.     do
  117.     {
  118.         WaitNextEvent (everyEvent & ~highLevelEventMask, &event, GetCaretTime( ), nil);
  119.  
  120.         switch (event.what)
  121.         {
  122.             case mouseDown:
  123.  
  124.                 partCode = FindWindow (event.where, &whichWindow);
  125.  
  126.                 if (whichWindow != GetDialogWindow (pop))
  127.                 {
  128.                     #if TARGET_API_MAC_CARBON
  129.                         SysBeep(10);
  130.                     #else
  131.                         if (partCode == inSysWindow)
  132.                             SystemClick (&event,whichWindow);
  133.                         else
  134.                             SysBeep(10);
  135.                     #endif
  136.                     
  137.                     break;
  138.                 }
  139.  
  140.                 if (inDrag == partCode)
  141.                 {
  142.                     Rect dragBounds;
  143.  
  144.  
  145.                     dragBounds = (**GetMainDevice ( )).gdRect;
  146.                     InsetRect (&dragBounds, 4, 4);
  147.                     DragWindow (GetDialogWindow (pop), event.where, &dragBounds);
  148.                     break;
  149.                 }
  150.  
  151.                 // fall thru
  152.  
  153.             default:
  154.  
  155.                 if (mfp)
  156.                     handledIt = CallModalFilterProc (mfp,pop,&event,itemHit);
  157.  
  158.                 if (!handledIt && IsDialogEvent(&event))
  159.                     DialogSelect(&event,&dummy,itemHit);
  160.  
  161.                 break;
  162.         }
  163.     }
  164.     while (*itemHit == kDialogItemIndexNone);
  165. }
  166.  
  167. pascal Boolean ToggleDialogCheckBox (DialogRef dialog, DialogItemIndex itemIndex)
  168. {
  169.     if (!MoreAssert (itemIndex > 0 && itemIndex <= CountDITL (dialog)))
  170.         return false;
  171.  
  172.     short iType; Handle iHandle; Rect iRect;
  173.     ::GetDialogItem (dialog, itemIndex, &iType, &iHandle, &iRect);
  174.  
  175.     if (!MoreAssert ((iType & ~(kItemDisableBit | kControlDialogItem)) == chkCtrl))
  176.         return false;
  177.  
  178.     Boolean result = !::GetControlValue (ControlRef (iHandle));
  179.     ::SetControlValue (ControlRef (iHandle), result);
  180.  
  181.     return result;
  182. }
  183.  
  184. pascal OSErr MoreAppendDialogItemList (DialogPtr dialog, DialogItemIndex ditlID, DITLMethod method)
  185. {
  186.     //
  187.     //    Under Mac OS 8.5 and 8.5.1, AppendDialogItemList returns garbage when called
  188.     //    from 68K code (through the address in the trap table). We attempt to detect
  189.     //    the context in which this is a problem and work around it by doing our own
  190.     //    feeble but not grossly incorrect error checking. One thing to be aware of
  191.     //    is that this function does not allow you to append 0-item dialog item lists
  192.     //    on systems which have the bug. If you have a better idea for how to do a
  193.     //    modicum of error checking without a significant increase in complexity,
  194.     //    let us know.
  195.     //
  196.  
  197.     #if !TARGET_CPU_68K
  198.         return AppendDialogItemList (dialog,ditlID,method);
  199.     #else
  200.         Boolean appendDialogItemListReturnsGarbage = (GetSystemVersion ( ) & 0xFFFFFFF0) == 0x00000850;
  201.  
  202.         if (!appendDialogItemListReturnsGarbage)
  203.             return AppendDialogItemList (dialog,ditlID,method);
  204.  
  205.         DialogItemIndex        itemCountBefore        = CountDITL (dialog);
  206.         OSErr                err                    = AppendDialogItemList (dialog,ditlID,method);
  207.  
  208.         if (err)
  209.         {
  210.             DialogItemIndex itemCountAfter = CountDITL (dialog);
  211.             err = (itemCountBefore < itemCountAfter) ? noErr : paramErr;
  212.         }
  213.  
  214.         return err;
  215.     #endif
  216. }
  217.